package com.example.prison.controller;

import com.example.prison.config.Response;
import com.example.prison.entity.HistoricalAlarmData;
import com.example.prison.service.HistoricalAlarmDataService;
import com.example.prison.service.SystemLogService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api(tags = "历史报警数据管理")
@RestController
@RequestMapping("/api")
public class HistoricalAlarmDataController {

    @Autowired
    private SystemLogService systemLogService;

    @Autowired
    private HistoricalAlarmDataService historicalAlarmDataService;

    @ApiOperation("获取历史报警数据列表")
    @GetMapping("/getHistoricalAlarmData")
    public Response<PageInfo<HistoricalAlarmData>> getHistoricalAlarmData(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                               @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                               @RequestParam(value = "defenseSection", defaultValue = "") String defenseSection,
                                                               @RequestParam(value = "startTime", defaultValue = "") String startTime,
                                                               @RequestParam(value = "endTime", defaultValue = "") String endTime) {
        log.info("获取历史报警数据列表");
        PageInfo<HistoricalAlarmData> pageInfo = historicalAlarmDataService.get(pageNumber, pageSize, defenseSection, startTime, endTime);
        return Response.success("获取成功！", pageInfo);
    }

    @ApiOperation("新增历史报警数据")
    @PostMapping("/addHistoricalAlarmData")
    public Response<?> addHistoricalAlarmData(@RequestBody HistoricalAlarmData area) {
        log.info("新增历史报警数据");
        int result = historicalAlarmDataService.add(area);
        if (result == -1) {
            return Response.error("防区已存在！");
        }
        return Response.success("添加成功！");
    }

    @ApiOperation("删除历史报警数据")
    @PostMapping("/deleteHistoricalAlarmData")
    public Response<?> deleteHistoricalAlarmData(@Parameter(description = "防区ID") @RequestBody Integer[] ids) {
        int result = historicalAlarmDataService.delete(ids);
        log.info("删除历史报警数据");
        systemLogService.add("删除历史报警数据");
        return Response.success("删除成功！");
    }

    @ApiOperation("编辑历史报警数据")
    @PostMapping("/updateHistoricalAlarmData")
    public Response<?> updateArea(@RequestBody HistoricalAlarmData area) {
        Integer id = area.getId();
        int result = historicalAlarmDataService.update(area, id);
        log.info("编辑历史报警数据");
        return Response.success("更新成功！");
    }

    @ApiOperation("获取一条历史报警数据")
    @GetMapping("/getOneHistoricalAlarmData")
    public Response<?> getOneHistoricalAlarmData(@RequestParam(value = "defenseSection", defaultValue = "") String defenseSection,
                                                                          @RequestParam(value = "alarmLevel", defaultValue = "") String alarmLevel,
                                                                          @RequestParam(value = "alarmStatus", defaultValue = "") String alarmStatus) {
        log.info("获取历史报警数据列表");
        HistoricalAlarmData pageInfo = historicalAlarmDataService.getOneAlarmData(defenseSection, alarmLevel, alarmStatus);
        return Response.success("获取成功！", pageInfo);
    }

}
